import pika

# 建立与 RabbitMQ 服务器的连接
connection = pika.BlockingConnection(pika.URLParameters('amqp://guest:guest@192.168.110.135:5672'))
channel = connection.channel()

# 声明一个 fanout 类型的 exchange（与生产者保持一致）
channel.exchange_declare(exchange='logs', exchange_type='fanout')

# 创建一个临时队列，该队列是独占的且会在断开连接时自动删除
result = channel.queue_declare('', exclusive=True)
queue_name = result.method.queue

# 将队列绑定到 fanout exchange，无需指定 routing key
channel.queue_bind(exchange='logs', queue=queue_name)

print(' [*] Waiting for messages. To exit press CTRL+C')

# 定义消息处理的回调函数
def callback(ch, method, properties, body):
    print(f" [x] Received {body}")

# 开始消费
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()